package com.github.catvod.spider.merge;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Locale;

/* loaded from: classes.dex */
public final class UF {
    private int BQ;
    private int I;
    private Reader V0;
    private String[] W;
    private char[] h;
    private int j;
    private int oC;
    private int q;
    private boolean u;

    public UF(Reader reader, int i) {
        this.BQ = -1;
        this.W = new String[512];
        R2.u(reader);
        R2.j(reader.markSupported());
        this.V0 = reader;
        this.h = new char[Math.min(i, 32768)];
        V0();
    }

    static boolean To(char[] cArr, int i, int i2, String str) {
        if (i2 != str.length()) {
            return false;
        }
        int i3 = 0;
        while (true) {
            int i4 = i2 - 1;
            if (i2 == 0) {
                return true;
            }
            int i5 = i + 1;
            int i6 = i3 + 1;
            if (cArr[i] != str.charAt(i3)) {
                return false;
            }
            i = i5;
            i2 = i4;
            i3 = i6;
        }
    }

    private void V0() {
        int i;
        int i2;
        boolean z;
        if (this.u || (i = this.I) < this.q) {
            return;
        }
        int i3 = this.BQ;
        if (i3 != -1) {
            i2 = i - i3;
            i = i3;
        } else {
            i2 = 0;
        }
        try {
            long j = i;
            long skip = this.V0.skip(j);
            this.V0.mark(32768);
            int i4 = 0;
            while (true) {
                z = true;
                if (i4 > 1024) {
                    break;
                }
                Reader reader = this.V0;
                char[] cArr = this.h;
                int read = reader.read(cArr, i4, cArr.length - i4);
                if (read == -1) {
                    this.u = true;
                }
                if (read <= 0) {
                    break;
                } else {
                    i4 += read;
                }
            }
            this.V0.reset();
            if (i4 > 0) {
                if (skip != j) {
                    z = false;
                }
                R2.j(z);
                this.j = i4;
                this.oC += i;
                this.I = i2;
                if (this.BQ != -1) {
                    this.BQ = 0;
                }
                this.q = Math.min(i4, 24576);
            }
        } catch (IOException e) {
            throw new m(e);
        }
    }

    private static String j(char[] cArr, String[] strArr, int i, int i2) {
        if (i2 > 12) {
            return new String(cArr, i, i2);
        }
        if (i2 < 1) {
            return "";
        }
        int i3 = i2 * 31;
        int i4 = 0;
        int i5 = i;
        while (i4 < i2) {
            i3 = (i3 * 31) + cArr[i5];
            i4++;
            i5++;
        }
        int i6 = i3 & 511;
        String str = strArr[i6];
        if (str == null) {
            String str2 = new String(cArr, i, i2);
            strArr[i6] = str2;
            return str2;
        }
        if (To(cArr, i, i2, str)) {
            return str;
        }
        String str3 = new String(cArr, i, i2);
        strArr[i6] = str3;
        return str3;
    }

    private boolean yx() {
        return this.I >= this.j;
    }

    String BQ() {
        int i = this.I;
        int i2 = this.j;
        char[] cArr = this.h;
        int i3 = i;
        while (i3 < i2) {
            char c = cArr[i3];
            if (c == 0 || c == '&' || c == '<') {
                break;
            }
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    boolean C4(char... cArr) {
        if (PU()) {
            return false;
        }
        V0();
        char c = this.h[this.I];
        for (char c2 : cArr) {
            if (c2 == c) {
                return true;
            }
        }
        return false;
    }

    void F() {
        int i = this.I;
        if (i < 1) {
            throw new m(new IOException("WTF: No buffer left to unconsume."));
        }
        this.I = i - 1;
    }

    String F8() {
        int i = this.I;
        int i2 = this.j;
        char[] cArr = this.h;
        int i3 = i;
        while (i3 < i2) {
            char c = cArr[i3];
            if (c == 0 || c == '<') {
                break;
            }
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    public char GM() {
        V0();
        if (yx()) {
            return (char) 65535;
        }
        return this.h[this.I];
    }

    void H9() {
        this.BQ = -1;
    }

    char I() {
        V0();
        char c = yx() ? (char) 65535 : this.h[this.I];
        this.I++;
        return c;
    }

    boolean Ig() {
        char c;
        return !PU() && (c = this.h[this.I]) >= '0' && c <= '9';
    }

    boolean LW(String str) {
        V0();
        if (!b(str)) {
            return false;
        }
        this.I += str.length();
        return true;
    }

    boolean Og(String str) {
        if (!l(str)) {
            return false;
        }
        this.I += str.length();
        return true;
    }

    String PD() {
        V0();
        char[] cArr = this.h;
        String[] strArr = this.W;
        int i = this.I;
        String j = j(cArr, strArr, i, this.j - i);
        this.I = this.j;
        return j;
    }

    public boolean PU() {
        V0();
        return this.I >= this.j;
    }

    String Qy() {
        char c;
        V0();
        int i = this.I;
        while (true) {
            int i2 = this.I;
            if (i2 >= this.j || (((c = this.h[i2]) < 'A' || c > 'Z') && ((c < 'a' || c > 'z') && !Character.isLetter(c)))) {
                break;
            }
            this.I++;
        }
        return j(this.h, this.W, i, this.I - i);
    }

    boolean R(char c) {
        return !PU() && this.h[this.I] == c;
    }

    int T(CharSequence charSequence) {
        V0();
        char charAt = charSequence.charAt(0);
        int i = this.I;
        while (i < this.j) {
            if (charAt != this.h[i]) {
                do {
                    i++;
                    if (i >= this.j) {
                        break;
                    }
                } while (charAt != this.h[i]);
            }
            int i2 = i + 1;
            int length = (charSequence.length() + i2) - 1;
            int i3 = this.j;
            if (i < i3 && length <= i3) {
                int i4 = i2;
                for (int i5 = 1; i4 < length && charSequence.charAt(i5) == this.h[i4]; i5++) {
                    i4++;
                }
                if (i4 == length) {
                    return i - this.I;
                }
            }
            i = i2;
        }
        return -1;
    }

    public int V() {
        return this.oC + this.I;
    }

    String W() {
        int i;
        char c;
        V0();
        int i2 = this.I;
        while (true) {
            i = this.I;
            if (i >= this.j || (c = this.h[i]) < '0' || c > '9') {
                break;
            }
            this.I = i + 1;
        }
        return j(this.h, this.W, i2, i - i2);
    }

    boolean b(String str) {
        V0();
        int length = str.length();
        if (length > this.j - this.I) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (str.charAt(i) != this.h[this.I + i]) {
                return false;
            }
        }
        return true;
    }

    boolean f(char[] cArr) {
        V0();
        return !PU() && Arrays.binarySearch(cArr, this.h[this.I]) >= 0;
    }

    public void h() {
        this.I++;
    }

    public String i4(char... cArr) {
        V0();
        int i = this.I;
        int i2 = this.j;
        char[] cArr2 = this.h;
        int i3 = i;
        loop0: while (i3 < i2) {
            for (char c : cArr) {
                if (cArr2[i3] == c) {
                    break loop0;
                }
            }
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    String iU(String str) {
        int T = T(str);
        if (T != -1) {
            String j = j(this.h, this.W, this.I, T);
            this.I += T;
            return j;
        }
        if (this.j - this.I < str.length()) {
            return PD();
        }
        int length = (this.j - str.length()) + 1;
        char[] cArr = this.h;
        String[] strArr = this.W;
        int i = this.I;
        String j2 = j(cArr, strArr, i, length - i);
        this.I = length;
        return j2;
    }

    boolean j5(String str) {
        Locale locale = Locale.ENGLISH;
        return T(str.toLowerCase(locale)) > -1 || T(str.toUpperCase(locale)) > -1;
    }

    boolean l(String str) {
        V0();
        int length = str.length();
        if (length > this.j - this.I) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (Character.toUpperCase(str.charAt(i)) != Character.toUpperCase(this.h[this.I + i])) {
                return false;
            }
        }
        return true;
    }

    public String n(char c) {
        int rd = rd(c);
        if (rd == -1) {
            return PD();
        }
        String j = j(this.h, this.W, this.I, rd);
        this.I += rd;
        return j;
    }

    boolean nf() {
        if (PU()) {
            return false;
        }
        char c = this.h[this.I];
        return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || Character.isLetter(c);
    }

    String oC(boolean z) {
        int i = this.I;
        int i2 = this.j;
        char[] cArr = this.h;
        int i3 = i;
        while (i3 < i2) {
            char c = cArr[i3];
            if (c == 0) {
                break;
            }
            if (c != '\"') {
                if (c == '&') {
                    break;
                }
                if (c != '\'') {
                    continue;
                    i3++;
                } else if (z) {
                    break;
                }
            }
            if (!z) {
                break;
            }
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    String oq() {
        char c;
        V0();
        int i = this.I;
        while (true) {
            int i2 = this.I;
            if (i2 >= this.j || (((c = this.h[i2]) < 'A' || c > 'Z') && ((c < 'a' || c > 'z') && !Character.isLetter(c)))) {
                break;
            }
            this.I++;
        }
        while (!yx()) {
            char[] cArr = this.h;
            int i3 = this.I;
            char c2 = cArr[i3];
            if (c2 < '0' || c2 > '9') {
                break;
            }
            this.I = i3 + 1;
        }
        return j(this.h, this.W, i, this.I - i);
    }

    public void q() {
        Reader reader = this.V0;
        if (reader == null) {
            return;
        }
        try {
            reader.close();
        } catch (IOException unused) {
        } catch (Throwable th) {
            this.V0 = null;
            this.h = null;
            this.W = null;
            throw th;
        }
        this.V0 = null;
        this.h = null;
        this.W = null;
    }

    int rd(char c) {
        V0();
        for (int i = this.I; i < this.j; i++) {
            if (c == this.h[i]) {
                return i - this.I;
            }
        }
        return -1;
    }

    String t() {
        V0();
        int i = this.I;
        int i2 = this.j;
        char[] cArr = this.h;
        int i3 = i;
        while (i3 < i2) {
            char c = cArr[i3];
            if (c == 0 || c == ' ' || c == '/' || c == '<' || c == '>' || c == '\t' || c == '\n' || c == '\f' || c == '\r') {
                break;
            }
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    public String toString() {
        if (this.j - this.I < 0) {
            return "";
        }
        char[] cArr = this.h;
        int i = this.I;
        return new String(cArr, i, this.j - i);
    }

    String u() {
        int i;
        char c;
        V0();
        int i2 = this.I;
        while (true) {
            i = this.I;
            if (i >= this.j || (((c = this.h[i]) < '0' || c > '9') && ((c < 'A' || c > 'F') && (c < 'a' || c > 'f')))) {
                break;
            }
            this.I = i + 1;
        }
        return j(this.h, this.W, i2, i - i2);
    }

    void w4() {
        if (this.j - this.I < 1024) {
            this.q = 0;
        }
        V0();
        this.BQ = this.I;
    }

    String y(char... cArr) {
        V0();
        int i = this.I;
        int i2 = this.j;
        char[] cArr2 = this.h;
        int i3 = i;
        while (i3 < i2 && Arrays.binarySearch(cArr, cArr2[i3]) < 0) {
            i3++;
        }
        this.I = i3;
        return i3 > i ? j(this.h, this.W, i, i3 - i) : "";
    }

    void z() {
        int i = this.BQ;
        if (i == -1) {
            throw new m(new IOException("Mark invalid"));
        }
        this.I = i;
        H9();
    }

    public UF(Reader reader) {
        this(reader, 32768);
    }

    public UF(String str) {
        this(new StringReader(str), str.length());
    }
}
